package com.sanley.piss.web.handle;

import com.gitee.dreamkaylee.shiro.authc.exception.ExpiredAccountException;
import com.gitee.dreamkaylee.shiro.authc.exception.NoneRoleException;
import com.sanley.piss.model.exceptionUtil.exception.ResultEntity;
import com.sanley.piss.model.exceptionUtil.exception.ServiceException;
import com.sanley.piss.model.exceptionUtil.json.ResultEnum;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create By  on 2021/3/5.
 */
@RestControllerAdvice
public class SinopecWebHandle {
    private Logger logger = LoggerFactory.getLogger("SinopecWebHandle");

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    public String exceptionHandler(HttpServletRequest req, NullPointerException e) {
        logger.error("发生空指针异常！原因是:", e);
        return "html/error/500";
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(HttpServletRequest req, Exception e) {
        logger.error("未知异常！原因是:", e);
        return "html/error/500";
    }

    @ExceptionHandler(value = UnknownAccountException.class)
    public ResultEntity exceptionHandler(HttpServletRequest req, UnknownAccountException e) {
        logger.error("账号密码错误", e);
        return ResultEntity.error(ResultEnum.loginFail);
    }

    @ExceptionHandler(value = {LockedAccountException.class, DisabledAccountException.class, ExpiredAccountException.class})
    public ResultEntity exceptionHandler(HttpServletRequest req, DisabledAccountException e) {
        logger.error("账号被禁用:", e);
        return ResultEntity.error(ResultEnum.userDisabled);
    }

    @ExceptionHandler(value = ServiceException.class)
    public ResultEntity exceptionHandler(HttpServletRequest req, ServiceException e) {
        logger.error(e.getMessage(), e);
        return ResultEntity.error(e.getErrorMsg(), e.getErrorCode());
    }

    @ExceptionHandler(value = NoneRoleException.class)
    public ResultEntity exceptionHandler(HttpServletRequest req, NoneRoleException e) {
        logger.error(e.getMessage(), e);
        return ResultEntity.error(ResultEnum.roleIsNull);
    }

    @ExceptionHandler(value = BindException.class)
    public ResultEntity handleMethodArgumentNotValidException(BindException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
        logger.error(message, e);
        return ResultEntity.error(message);
    }


    @ExceptionHandler(value = UnauthorizedException.class)
    public ResultEntity handleMethodArgumentNotValidException(UnauthorizedException e) {
        logger.error(e.getMessage(), e);
        return ResultEntity.error(ResultEnum.roleIsNull);
    }

}
